import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class Player extends Thread{

	static final int PLAYER_WIDTH  = 20;
	static final int PLAYER_HEIGHT = 10;
	
	static final int PLAYER_STATUS_ALIVE 	= 10000;
	static final int PLAYER_STATUS_DEAD	 	= 10001;
	static final int PLAYER_STATUS_CRUSHED  = 10002;
	
	Point playerPosition;
	Point _movementPt;
	Rectangle playerRect;
	
	int playerStatus = PLAYER_STATUS_DEAD;
	
	boolean fireStatus = false;
	
	ArrayList<Bullet> bulletList = new ArrayList<Bullet>();
	
	public ArrayList<Bullet> getMissileList()
	{
		return bulletList;
	}
	
	public Player(int _centerOfWindowX, int _centerOfWindowY)
	{
		playerPosition = new Point(_centerOfWindowX,_centerOfWindowY);
		_movementPt = new Point(0,0);
		playerRect = new Rectangle();
		playerStatus = PLAYER_STATUS_ALIVE;
	}
	
	public Player()
	{
		playerPosition = new Point(100,100);
		_movementPt = new Point(0,0);
		playerRect = new Rectangle();
		playerStatus = PLAYER_STATUS_ALIVE;
	}
	
	public Rectangle getPlayerRectangle()
	{
		playerRect.x = playerPosition.x;
		playerRect.y = playerPosition.y;
		playerRect.width = PLAYER_WIDTH;
		playerRect.height = PLAYER_HEIGHT;
		
		return playerRect;
	}
	public Point GetPlayerPosition()
	{
		return playerPosition;
	}
	
	public void KeyReleased(KeyEvent e)
	{
		if ( e.getKeyCode() == KeyEvent.VK_SPACE )
		{
			fireStatus = false;
		}
		
		if ( e.getKeyCode() == KeyEvent.VK_UP )
		{
			_movementPt.y = 0;
		}
		if ( e.getKeyCode() == KeyEvent.VK_DOWN )
		{
			_movementPt.y = 0;
		}
		if ( e.getKeyCode() == KeyEvent.VK_LEFT )
		{
			_movementPt.x = 0;
		}
		if ( e.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			_movementPt.x = 0;
		}
	}
	
	public void keyPressed(KeyEvent e)
	{
		if ( e.getKeyCode() == KeyEvent.VK_SPACE )
		{
			fireStatus = true;
		}
		if ( e.getKeyCode() == KeyEvent.VK_UP )
		{
			
			_movementPt.y = -5;
		}
		if ( e.getKeyCode() == KeyEvent.VK_DOWN )
		{
			_movementPt.y = +5;
		}
		if ( e.getKeyCode() == KeyEvent.VK_LEFT )
		{
			_movementPt.x = -5;
		}
		if ( e.getKeyCode() == KeyEvent.VK_RIGHT )
		{
			_movementPt.x = 5;
		}
		
	}
	
	public void render(Graphics g)
	{
		g.drawRect(playerPosition.x , playerPosition.y, PLAYER_WIDTH, PLAYER_HEIGHT);
		for( int i = 0 ; i < bulletList.size(); i++ )
		{
			g.drawOval(((int)bulletList.get(i).getMissilePosition().x), 
					((int)bulletList.get(i).getMissilePosition().y) ,
					Bullet.BULLET_WIDTH , Bullet.BULLET_HEIGHT);
		}
	}

	public boolean getFireState()
	{
		return fireStatus;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
	}

	public void shootBullet(Point _destinationPoint)
	{
		bulletList.add(new Bullet(new Point(playerPosition.x, playerPosition.y),_destinationPoint));
	}
	
	public void update() {
		
		playerPosition.x = playerPosition.x + _movementPt.x;
		playerPosition.y = playerPosition.y + _movementPt.y;
		
		if ( playerStatus == Player.PLAYER_STATUS_CRUSHED )
		{
			//업데이트에서 이전 프레임(시간)을 저장하고 일정시간 애니매이션 발동후 소멸
			playerStatus = Player.PLAYER_STATUS_DEAD;
		}
		
		if ( playerStatus == Player.PLAYER_STATUS_DEAD )
		{
			//게임오버 애니메이션
			System.out.println("게임오버");
		}
		
		if ( fireStatus )
		{
			shootBullet(playerPosition);
		}
		
		for (int i = 0 ; i < bulletList.size(); i ++ )
		{
			bulletList.get(i).update();
			if ( bulletList.get(i).getBulletStatus() == Bullet.STATUS_DEAD )
			{
				bulletList.remove(i--);
			}
		}
	}
	public void setPlayerStatus(int _playerStatus)
	{ 
		playerStatus = _playerStatus;
	}
	
	public int getPlayerStatus()
	{
		return playerStatus;
	}
}
